package com.umeox.watch.moto.launcher.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import com.umeox.moto.watch.themes.app.ThemeAppCompatActivity;
import com.umeox.moto.watch.themes.widget.ImageView;
import com.umeox.moto.watch.themes.widget.Slider;
import com.umeox.watch.moto.launcher.R;


public class VolumeActivity extends ThemeAppCompatActivity implements View.OnTouchListener {

    private static final String TAG = VolumeActivity.class.getSimpleName();

    private Slider mAlarmSlider;
    private Slider mMediaSlider;
    private Slider mRingSlider;

    private AudioManager mAudioManager;
    private int mAlarmCurrentVolume;
    private int mAlarmMaxVolume;
    private int mMediaCurrentVolume;

    private int mMediaMaxVolume;

    private int mRingCurrentVolume;
    private int mRingMaxVolume;
    private ImageView mRingIconView;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_volume);

        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
        mAlarmMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM);
        mAlarmCurrentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_ALARM);


        mMediaMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
        mMediaCurrentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

        mRingMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
        mRingCurrentVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);

        initView();
    }

    @SuppressLint("ClickableViewAccessibility")
    private void initView() {

        mRingIconView = findViewById(R.id.iv_ring_icon);


        mAlarmSlider = findViewById(R.id.sl_volume_alarm);
        mMediaSlider = findViewById(R.id.sl_volume_media);
        mRingSlider = findViewById(R.id.sl_volume_ring);

        mRingSlider.setOnTouchListener(this);
        mAlarmSlider.setOnTouchListener(this);
        mMediaSlider.setOnTouchListener(this);

        mAlarmSlider.setValueRange(0, mAlarmMaxVolume, true);
        mAlarmSlider.setValue(mAlarmCurrentVolume, true);

        mMediaSlider.setValueRange(0, mMediaMaxVolume, true);
        mMediaSlider.setValue(mMediaCurrentVolume, true);

        mRingSlider.setValueRange(0, mRingMaxVolume, true);
        mRingSlider.setValue(mRingCurrentVolume, true);

        mAlarmSlider.setOnPositionChangeListener((view, fromUser, oldPos, newPos, oldValue, newValue) -> {

            mAudioManager.setStreamVolume(AudioManager.STREAM_ALARM, newValue, 0);
        });

        mMediaSlider.setOnPositionChangeListener((view, fromUser, oldPos, newPos, oldValue, newValue) -> {


            mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, newValue, 0);
        });

        mRingSlider.setOnPositionChangeListener((view, fromUser, oldPos, newPos, oldValue, newValue) -> {

            mAudioManager.setStreamVolume(AudioManager.STREAM_RING, newValue, 0);
        });
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, VolumeActivity.class);
        context.startActivity(intent);
    }


    @Override
    public boolean onTouch(View v, MotionEvent event) {

        if (event.getAction() != MotionEvent.ACTION_DOWN) {
            return false;
        }
        final int id = v.getId();
        int resId;
        switch (id) {
            case R.id.sl_volume_ring:
                resId = R.drawable.icon_volume_ring;
                mRingSlider.onchangeState(true);
                break;
            case R.id.sl_volume_alarm:
                mAlarmSlider.onchangeState(true);
                resId = R.drawable.icon_volume_alarm;
                break;
            case R.id.sl_volume_media:
                mMediaSlider.onchangeState(true);
                resId = R.drawable.icon_volume_media;
                break;
            default:
                resId = R.drawable.icon_volume_default;
                break;

        }
        mRingIconView.setImageResource(resId);

        return false;
    }
}
